import { handleSearchLoading } from "@userList/search";
import { IMOptionsType } from "@/types/dict";
import {
  columns,
  defaultColumns,
  defaultSearchList,
  drawerFormState,
  resetFormState,
  searchList,
  tabPower,
  tableActiveKey,
} from "./store";
import { handleAction, refreshTable } from ".";
import { PayRouteChildComponent } from "@/routes/basics.router";
import { getSelfNodeIcon } from "@system/menu/store";
const searchLoading = ref(false);
const resetLoading = ref(false);
const handleSwitchChange = async (
  checked: boolean | string | number,
  event: Event
) => {
  try {
    const res: any = await gradeApi.updateAddFriendIsMember({
      addFriendIsMember: checked === true ? 0 : 1,
    });
    if (res.code != 200) return message.error(res.message);
    message.success($t("base.updateSucess"));
  } catch (error: any) {
    message.error(error.message);
  }
};
export const searchConfig = () => {
  const router = useRouter();
  const memberLevelTab = (
    <>
      <a-space size={5}>
        <a-switch
          v-model:checked={drawerFormState.addFriendIsMember}
          checked-children={$t("base.on")}
          un-checked-children={$t("base.off")}
          onChange={handleSwitchChange}
        />
        <tips-cell content={[$t("grade41"), $t("grade42")]} />
      </a-space>
      {withDirectivePower({
        vNode: (
          <s-button
            type="primary"
            onClick={() => handleAction({ type: "add" })}
            icon={renderIcon(getSelfNodeIcon("gradeApi.createVip"))}
          >
            {$t("base.addMemberLevel")}
          </s-button>
        ),
        bindingValue: "gradeApi.createVip",
      })}

      {withDirectivePower({
        vNode: (
          <s-button
            type="primary"
            onClick={() =>
              $addTags({
                name: $t("grade12"),
                path: PayRouteChildComponent.path,
                callback: () => router.push(PayRouteChildComponent.path),
              })
            }
            icon={renderIcon(getSelfNodeIcon("payApi.query"))}
          >
            {$t("base.payRecord")}
          </s-button>
        ),
        bindingValue: "payApi.query",
      })}
    </>
  );
  const popularLevelTab = withDirectivePower({
    vNode: (
      <s-button
        type="primary"
        onClick={() => handleAction({ type: "add" })}
        icon={renderIcon(getSelfNodeIcon("gradeApi.createPopular"))}
      >
        {$t("base.addPopularLevel")}
      </s-button>
    ),
    bindingValue: "gradeApi.createPopular",
  });
  const scoreLevelTab = withDirectivePower({
    vNode: (
      <s-button
        type="primary"
        onClick={() => handleAction({ type: "add" })}
        icon={renderIcon(getSelfNodeIcon("gradeApi.createScore"))}
      >
        {$t("base.addScoreLevel")}
      </s-button>
    ),
    bindingValue: "gradeApi.createScore",
  });
  const defineSearch = {
    okText: getProxyT("base.query"),
    cancelText: getProxyT("base.reset"),
    searchLoading,
    resetLoading,
    refreshTableData: async (searchParams: any, type: any) => {
      handleSearchLoading(true, type, defineSearch);
      await refreshTable(searchParams);
      handleSearchLoading(false, type, defineSearch);
    },
    extra: () => {
      switch (tableActiveKey.value) {
        case 0:
          return memberLevelTab;
        case 1:
          return popularLevelTab;
        case 2:
          return scoreLevelTab;
      }
    },
    tabsSlot: {
      activeKey: tableActiveKey,
      on: {
        change(tableActiveKey: any) {
          initObjAttr(columns, defaultColumns[tableActiveKey]);
          resetFormState(drawerFormState);
          searchList.value = defaultSearchList[tableActiveKey];
          refreshTable();
        },
      },
      options: ([] as IMOptionsType[])
        .concat(
          tabPower[0]
            ? {
                label: getProxyT("table5"),
                value: 0,
              }
            : []
        )
        .concat(
          tabPower[1]
            ? {
                label: getProxyT("table6"),
                value: 1,
              }
            : []
        )
        .concat(
          tabPower[2]
            ? {
                label: getProxyT("table7"),
                value: 2,
              }
            : []
        ),
    },
    searchList: searchList,
  };
  return defineSearch;
};
